using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using Core.SimpleTemp.Repositories.Internal;
using Core.SimpleTemp.Entitys;

namespace Core.SimpleTemp.Api.PC.Api
{
    /// <summary>
    /// PX_Plan
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class PX_PlanController : CoreApiController
    {
        private readonly PX_PlanService _service;
        private CoreDBContext _coreDBContext;
        /// <summary>
        /// PX_Plan
        ///</summary>
        public PX_PlanController(PX_PlanService service, CoreDBContext coreDBContext)
        {
            _service = service;
            _coreDBContext = coreDBContext;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<PX_PlanDto>
                pagingQueryModel)
        {
            IPageModel<PX_PlanDto>
                result;


            result = await _service.GetList(pagingQueryModel);

            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("PCGetList")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.PCGetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            PCGetList(PagingModel<PX_PlanDto>
                pagingQueryModel)
        {
            IPageModel<PX_PlanDto>
                result;
            result = await _service.PCGetList(pagingQueryModel);

            result.PageData.ForEach(dto =>
           {
               var str = "";
               dto.PX_PlanUser.ToList().ForEach(a =>
               {
                   str += _coreDBContext.SysUserInfor.Where(x => x.SysUserID == a.PersonID).FirstOrDefault().Name + ",";
               });
               str = str.TrimEnd(',');

               dto.Mark02 = str;

           });

            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 发送培训通知
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("Notice")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.Notice), AuthorizationConst.SAVE)]
        public IActionResult Notice(Guid id)
        {
            string result = _service.Notice(id);
            if (result == "失败")
                return this.JsonFaild("当前时间已经大于培训开始时间,无法发送通知。请重新安排计划！");


            return this.JsonSuccess();

        }

        /// <summary>
        /// 完成培训操作
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("WCPX")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.WCPX), AuthorizationConst.SAVE)]
        public IActionResult WCPX(Guid id)
        {
            string result = _service.WCPX(id);


            return this.JsonSuccess();

        }


        /// <summary>
        /// 生成培训计划
        ///</summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpGet("SCPXJH")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.SCPXJH), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SCPXJH()
        {
            await _service.SCPXJH(UserIdentity.Factory.Value);
            return this.JsonSuccess();

        }




        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(PX_PlanDto dto)
        {

            dto.PX_Course = null;
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                dto.CreateFactory = UserIdentity.Factory;
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            bool b = false;
            foreach (Guid id in ids)
            {
                var obj = _coreDBContext.Set<PX_Plan>().Where(x => x.IsDeleted == false && x.Id == id).FirstOrDefault();
                if (obj != null && obj.State != "0")
                {

                    b = true;
                    break;
                }
            }
            if (b)
            {
                return this.JsonFaild("所选内容中有非未上报类型数据！");
            }

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("PCGet")]
        public async Task<IActionResult>
            PCGet(Guid id)
        {
            var model = await _service.PCGet(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }

        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<PX_PlanDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion

        /// <summary>
        /// 根据矩阵获取课程
        ///</summary>
        /// <param></param>
        /// <returns></returns>

        [HttpGet("GetCourse")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.GetCourse), AuthorizationConst.GET)]
        public async Task<IActionResult> GetCourse(Guid id)
        {
            var result = await _service.GetCourse(id);

            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 我的培训
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("WDPX")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.WDPX), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            WDPX(PagingModel<PX_PlanDto>
                pagingQueryModel)
        {
            IPageModel<PX_PlanDto>
                result;
            result = await _service.WDPX(pagingQueryModel,UserIdentity.Id);
            return this.JsonSuccess(result);
        }

        



        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("ExportWDPX")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.ExportWDPX), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            ExportWDPX(PagingModel<PX_PlanDto>
                pagingQueryModel)
        {
            var fileName = await _service.ExportWDPX(pagingQueryModel,UserIdentity.Id);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }


        /// <summary>
        /// 考试监控
        ///</summary>
        /// <param></param>
        /// <returns></returns>

        [HttpPost("KSJK")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.KSJK), "考试监控")]
        public async Task<IActionResult> KSJK(PagingModel<PX_PlanDto> pagingQueryModel)
        {
            var result = await _service.KSJK(pagingQueryModel, UserIdentity.Id);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 导出我的考试
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("ExportForKSJK")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.ExportForKSJK), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> ExportForKSJK(PagingModel<PX_PlanDto> pagingQueryModel)
        {
            var fileName = await _service.ExportForKSJK(pagingQueryModel, UserIdentity.Id);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        #region 用于定时任务的接口
        /// <summary>
        /// 生成计划数据接口调用
        ///</summary>
        /// <param></param>
        /// <returns></returns>

        [HttpGet("DealPlan")]
        [AllowAnonymous]
        public async Task<IActionResult> DealPlan()
        {
            //每天零晨1点
            await _service.DealPlan();
            return this.JsonSuccess();
        }


        #endregion
    }
}
